/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.transport.ptcp.psocket;

import java.io.IOException;
import java.io.InputStream;

import cgl.narada.transport.ptcp.psocket.threads.PTCPReceiverWorker;
import cgl.narada.transport.ptcp.psocket.tools.PTCPBuffer;
import cgl.narada.transport.ptcp.psocket.tools.PTCPParameters;

public class PTCPInputStream extends InputStream implements PTCPParameters {

  public PTCPBuffer buffer_ = null;
  private InputStream[] inputs_ = null; //internal list of streams
  private PTCPReceiverWorker[] prws_ = null;
  private boolean done_ = false;
  private int number_of_streams_ = 1; //number of streams
  private long tx_end_time_ = 0;
  private String module_name_ = "PTCPInputStream: ";

  /**
   * Default constructor
   * Construct an empty parallel input stream.
   */
  public PTCPInputStream(InputStream[] streams) {
    super();
     initialize(streams, false);
  }

  /**
   * Default constructor
   * Construct an empty parallel input stream with performance increase option
   */

  public PTCPInputStream(InputStream[] streams, boolean incPrf) {
    super();
    initialize(streams, incPrf);

  }

  private void initialize(InputStream[] streams, boolean incPrf) {
    int i;
    buffer_ = new PTCPBuffer(streams.length, incPrf);

    number_of_streams_ = streams.length;
    inputs_ = new InputStream[number_of_streams_];
    prws_ = new PTCPReceiverWorker[number_of_streams_];

    if (PTCPOutputStream_Debug) {
      System.out.println(module_name_ + "number of streams " +
                         number_of_streams_);
    }

    Thread worker = null;

    for (i = 0; i < streams.length; i++) {
      inputs_[i] = streams[i];
      prws_[i] = new PTCPReceiverWorker(this, streams[i], String.valueOf(i));
      worker = new Thread(prws_[i]);
      worker.start();
    }

  }

  public void setTxEndTime(long txEndTime) {
    this.tx_end_time_ = txEndTime;
  }

  public long getTxEndTime() {
    return tx_end_time_;
  }

  /**
   * Closes this input stream and releases any system resources
   * associated with the stream.
   *
   */
  public void close() throws IOException {
    if (inputs_ == null) {
      throw new IOException(module_name_+"No internal input streams.");
    }

    done_ = true;

    try {
      for (int i = 0; i < inputs_.length; i++) {
        inputs_[i].close();
      }
    }
    catch (IOException e) {
      throw e;
    }
  }

  public synchronized boolean finished() {
    return done_;

  }

  /**
   * Reads the next byte of data from the input stream. The value byte
   * is returned as an int in the range 0 to 255. If no byte is available
   * because the end of the stream has been reached,
   * the value -1 is returned. This method blocks until input data is
   * available, the end of thestream is detected, or an exception is thrown.
   *
   * This read only works if the input stream on the other end sends out
   * one byte of data, otherwise IOException will be thrown.
   *
   * Returns:
   *    the next byte of data, or -1 if the end of the stream is reached.
   */

  public synchronized int read() throws IOException {
    int value = 0;

    return value;
  }

  /**
   * Reads some number of bytes from the input stream and stores them
   * into the buffer array  b. The number of bytes actually read is returned
   * as an integer. This method blocks until input data is available,
   * end of file is detected, or an exception is thrown.
   *
   * If size of b is less than data size from input stream, an IOException
   * will be thrown.
   */
  public int read(byte[] b) throws IOException {
    int bytes = 0;

    try {
      bytes = read(b, 0, b.length);
    }
    catch (IOException e) {
      throw e;
    }

    return bytes;
  }

  /**
   * Reads up to len bytes of data from the input stream into an array of bytes.
   * An attempt is made to read as many as len bytes, but a smaller number
   * may be read, possibly zero. The number of bytes actually read is
   * returned as an integer.
   *
   * This method blocks until input data is available, end of file is detected,
   * or an exception is thrown.
   *
   * The parameter len must be larger than the size of data being sent over, or
   * an IOException is thrown.
   *
   * @param b - the data.
   * @param off - the start offset in the data.
   * @param length - the maximum number of bytes to read.
   */
  public synchronized int read(byte[] b, int off, int length) throws
      IOException {
    return 0;
  }

  public byte[] receive() throws InterruptedException {
    return buffer_.read();

  }
  public int getPacketSize() {
    return buffer_.getPacketSize();
  }

}
